package com.liang.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.liang.pojo.BoyFriend;
import com.liang.pojo.GirlFriend;
import com.liang.service.BoyFriendService;
import com.liang.service.GirlFriendService;

@Controller
public class BoyFriendController {
	@Autowired
	@Qualifier("boyFriendServiceImpl")
	private BoyFriendService boyFriendService;
	@Autowired
	@Qualifier("girlFriendServiceImpl")
	private GirlFriendService girlFriendService;
	
//	@RequestMapping("/love/selectBoyFriendList")
	@RequestMapping("/selectBoyFriendList")
	public String selectBoyFriendList(Model model) {
		List<BoyFriend> boyFriendList = boyFriendService.selectBoyFriendList();
		if(boyFriendList!=null) {
			model.addAttribute("boyFriends", boyFriendList);			return "boyFriend";
		}
		model.addAttribute("msg", "表里没有数据");
		return "error";
	}
	@RequestMapping("/selectBoyFriendBydetail")
	public String selectBoyFriendBydetail(String bdetail,Model model) {
		BoyFriend boyFriend = boyFriendService.selectBoyFriendBydetail(bdetail);
		if(boyFriend!=null) {
			List<BoyFriend> list=new ArrayList<BoyFriend>();
		list.add(boyFriend);
		model.addAttribute("boyFriends", list);
		return "boyFriend";
		}
		model.addAttribute("msg", "查不到对应的男朋友");
		return "error/error";
	}
	@RequestMapping("/insertBoyFriend")
	public String insertBoyFriend(BoyFriend boyFriend,Model model) {
		int id = boyFriend.getId();
		List<GirlFriend> girlFriendList = girlFriendService.selectGirlFriendList();
		List<Integer> num=new ArrayList<Integer>();
		for(int i=0;i<girlFriendList.size();i++) {
			num.add(girlFriendList.get(i).getGid());
		}
		for(int i=0;i<num.size();i++) {			
			if(num.get(i)==id) {
				boyFriendService.insertBoyFriend(boyFriend);
				return selectBoyFriendList(model);
			}
		}
		model.addAttribute("list", num);
		model.addAttribute("msg","只能在以下数字中做选择");
		return "love";
		
	}
	@RequestMapping("toUpdateBoyFriend/{bdetail}")
	public String toUpdateBoyFriend(@PathVariable("bdetail")String bdetail,Model model) {
		BoyFriend boyFriend = boyFriendService.selectBoyFriendBydetail(bdetail);
		model.addAttribute("boyFriend", boyFriend);
		return "toUpdateBoyFriend";
	}
	@RequestMapping("/updataBoyFriend")
	public String updateBoyFriend(BoyFriend boyFriend,Model model) {
		boyFriendService.updateBoyFriend(boyFriend);
		return selectBoyFriendList(model);
	}
	@RequestMapping("/deleteBoyFriend/{bage}")
	public String deleteBoyFriend(@PathVariable("bage")int bage,Model model) {
		boyFriendService.deleteBoyFriend(bage);
		return selectBoyFriendList(model);
	}
}
